<?php


if( class_exists( 'Clean' ) == false ) :

class InvalidValue extends Exception { }
class InvalidInt extends InvalidValue { }
class InvalidFloat extends InvalidValue { }
class InvalidString extends InvalidValue { }
class InvalidEmail extends InvalidValue { }

class Clean {
	
	public static function int( $i, $allowZero=false, $silent = false ) {
		
		if(is_numeric($i)) {
			
			$i = intval($i);
			if($allowZero === false && $i === 0) {
				
				if($silent) { return false; }
				else { throw new InvalidInt("Invalid zero integer"); }
			}
			
			return $i;
		}
		
		if($silent) { return false; }
		else { throw new InvalidInt("Invalid integer: '$i'"); }
	}
	
	public static function posInt( $i, $allowZero=false, $silent=false ) {
		
		$i = Clean::int( $i, $allowZero, $silent );
		
		if( $i === false) { return false; }
		
		if( $i < 0 ) {
			
			if( $silent ) { return false; }
			else { throw new InvalidInt( "Invalid negative integer"); }
			
		}
		
		return $i;
		
	}
	
	public static function intArray($arr, $allowZero=false, $silent = false) {
		
		if(is_array($arr) == false) {
			
			if($silent) { return false; }
			else { throw new InvalidInt("Invalid array"); }
		}
		
		$clean = array();
		foreach($arr as $key => $val) {
			
			$clean[$key] = Clean::int($val, $allowZero, $silent);
			if($silent && $clean[$key] === false) { return false; }
		}
		
		return $clean;
	}
	
	public static function float( $f, $allowZero=false, $silent=false ) {
		
		if( is_float($f) ) {
			$res = $f;
		}
		else if ( is_int($f) || ( is_string($f) && is_numeric($f) ) ) {
			$res = floatval($f);
		}
		
		if( isset( $res ) ) {
			
			if( $allowZero === false && $res === 0.0 ) {
				if( $silent ) { return false; }
				else { throw new InvalidFloat( "Invalid zero float" ); }
			}
			
			return $res;
		}		
			
		if( $silent ) { return false; }
		else { throw new InvalidFloat("Invalid float: '$f'"); }
	}
	
	public static function posFloat( $f, $allowZero=false, $silent=false ) {
		
		$f = Clean::float( $f, $allowZero, $silent );
		
		if( $f === false) { return false; }
		
		if( $f < 0 ) {
			
			if( $silent ) { return false; }
			else { throw new InvalidFloat( "Invalid negative float"); }
		}
		
		return $f;
		
		
	}

	
	public static function floatArray( $arr, $allowZero=false, $silent=false ) {
		
		if( is_array($arr) == false ) {
			if( $silent ) { return false; }
			else { throw new InvalidFloat("Invalid array"); }
		}
		
		$clean = array();
		foreach( $arr as $key => $val ) {
			
			$clean[$key] = Clean::float( $val, $allowZero, $silent );
			if( $silent && $clean[$key] === false ) { return false; }
		}
		
		return $clean;
	}
	
	public static function string($str, $allowEmpty=false, $allowTags=false, $silent=false) {
		if( is_string($str) == false ) {
			$str = strval($str);
		}
		if( $allowTags == false ) {
			$str = strip_tags($str);
		}
		if( $allowEmpty == false && $str === '' ) {
			if($silent) { return false; }
			else { throw new InvalidString("Invalid empty string."); }
		}
		return $str;
	}
	
	public static function stringArray( $arr, $allowEmpty=false, $allowTags=false, $silent=false ) {
		
		if( is_array( $arr ) == false ) {
			if( $silent ) { return false; }
			else { throw new InvalidString( "Invalid array" ); }
		}
		
		$clean = array();
		
		foreach( $arr as $key => $val ) {
			$clean[$key] = Clean::string( $val, $allowEmpty, $allowTags, $silent );
			if(!$allowEmpty && !$clean[$key])
				unset($clean[$key]);

			if( $silent && $clean[$key] === false ) { return false; }
		}
		
		return $clean;
	}
	
	public static function email( $email, $silent=false ) {
		
		if(is_string($email) === false || strlen($email) == 0) {
			if( $silent ) {
				return false;
			} else {
				throw new InvalidEmail("Invalid email address: '$email'");
			}
		}
		
		$qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
		
		$dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
		
		$atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
		
		$quoted_pair = '\\x5c\\x00-\\x7f';
		
		$domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
		
		$quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
		
		$domain_ref = $atom;
		
		$sub_domain = "($domain_ref|$domain_literal)";
		
		$word = "($atom|$quoted_string)";
		
		$domain = "$sub_domain(\\x2e$sub_domain)*";
		
		$local_part = "$word(\\x2e$word)*";
		
		$addr_spec = "$local_part\\x40$domain";
		
		$valid = (preg_match("!^$addr_spec$!D", $email) == 1);
		
		if($valid == false) {
			if( $silent) {
				return false;
			} else {
				throw new InvalidEmail("Invalid email address: '$email'");
			}
		}
		
		return $email;
	}
	
}

endif;

?>
